﻿// Custom sorting plugin
(function ($) {
    $.fn.sorted = function (customOptions) {
        var options = {
            reversed: false,
            by: function (a) { return a.text(); }
        };
        $.extend(options, customOptions);
        $data = $(this);
        arr = $data.get();
        arr.sort(function (a, b) {
            var valA = options.by($(a));
            var valB = options.by($(b));
            if (options.reversed) {
                return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
            } else {
                return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
            }
        });
        return $(arr);
    };
})(jQuery);

// DOMContentLoaded
$(function () {

    // bind radiobuttons in the form
    var $filterType = $('#filter input[name="type"]');
    var $filterYear = $('#filter input[name="year"]');
    var $filterSort = $('#filter input[name="sort"]');

    // get the first collection
    var $applications = $('ul.imagelist');

    // clone applications to get a second collection
    var $data = $applications.clone();

    // attempt to call Quicksand on every form change
    $('#filter input').change(function (e) {
        /*if ($($filterType + ':checked').val() == 'all') {
        var $filteredData = $data.find('li');
        } else {
        var $filteredData = $data.find('li[data-type=' + $($filterType + ":checked").val() + ']');
        }

        if ($($filterYear + ':checked').val() == 'all') {
        var $filteredData = $data.find('li');
        } else {
        var $filteredData = $data.find('li[data-year=' + $($filterYear + ":checked").val() + ']');
        }*/

        var $selecttype = $('#filter input[name="type"]:checked').val();
        var $selectyear = $('#filter input[name="year"]:checked').val();

        $data.each(function (i) {
            $(this).find('li').removeClass('gallerybox');
            if ($selecttype == 'all' && $selectyear == 'all') {
                var $filteredData = $(this).find('li');
            }
            else if ($selecttype == 'all' && $selectyear != 'all') {
                var $filteredData = $(this).find('li[data-year="' + $selectyear + '"]');
            }
            else if ($selecttype != 'all' && $selectyear == 'all') {
                var $filteredData = $(this).find('li[data-type="' + $selecttype + '"]');
            } else {
                var $filteredData = $(this).find('li[data-type="' + $selecttype + '"][data-year="' + $selectyear + '"]');
            }
            $filteredData.addClass('gallerybox')
            $(".gallerybox").fancybox({
                prevEffect: 'fade',
                nextEffect: 'fade',
                closeBtn: false,
                fitToView: true,
                autoSize: true,
                tpl: { wrap: '<div class="fancybox-wrap"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div>',
                    image: '<img class="fancybox-image" src="{href}" alt="" />',
                    error: '<p class="fancybox-error">请求出错.<br/>请过一段时间再重试.</p>',
                    closeBtn: '<div title="Close" class="fancybox-item fancybox-close"></div>',
                    next: '<a title="下一个" class="fancybox-item fancybox-next"><span></span></a>',
                    prev: '<a title="上一个" class="fancybox-item fancybox-prev"><span></span></a>'
                },
                helpers: {
                    title: { type: 'inside' },
                    buttons: {}
                }
            });
            // if sorted by size
            if ($('#filter input[name="sort"]:checked').val() == "year") {
                var $sortedData = $filteredData.sorted({
                    by: function (v) {
                        return parseFloat($(v).find('span[data-type=year]').text());
                    }
                });
            } else {
                // if sorted by name
                var $sortedData = $filteredData.sorted({
                    by: function (v) {
                        return $(v).find('strong').text().toLowerCase();
                    }
                });
            }

            // finally, call quicksand
            $applications.eq(i).quicksand($sortedData, {
                duration: 800,
                easing: 'easeInOutQuad'
            });
        });
        

    });

});